home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.lang.c,comp.programming,sci.math
- Subject: Re: Tough FACTORIAL math problem...
- Followup-To: comp.programming,sci.math
- Date: 15 Feb 96 16:07:25 GMT
- Organization: Clemson University
- Message-ID: <mjs.824400445@hubcap>
- References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> <4fv74c$chq@gatekeeper.alcatel.no> <4fvgbu$kmb@winx03.informatik.uni-wuerzburg.de>
- NNTP-Posting-Host: hubcap.clemson.edu
-
- schoof@informatik.uni-wuerzburg.de (Jochen Schoof) writes:
-
- >Ans now we should probably keep this away from comp.lang.c and turn to
- >some algorithmic group with it. Does anyone know one..?
-
- Comp.programming would be good. This is also appropriate for sci.math.
- I've directed followups to both groups.
-
- The original problem was to write a program to determine the rightmost
- nonzero digit of 1000!. Presumably this is to be done without
- computing too many of the remaining digits, and perhaps even without
- benefit of an arbitrary-precision arithmetic package (so the size of a
- representable integer is limited).
-
- The full text of Schoof's post follows (comp.lang.c readers can hit 'n'):
-
- %Sven Pran (Sven.Pran@alcatel.no) wrote:
- %: In article <31224679.6193@born.com>, John Cleland <clelaj@born.com> wrote:
- %: >
- %: >Don't just strip the trailing zeros, remove all digits except the last
- %: >non-zero digit (which is your output) and then multiply by the next number in
- %: >your sequence. This keeps the numbers down to a managable level and gives the
- %: >correct answer as no more significant digit can affect the value of the LSD.
- %: >
- %: . . . .
- %:
- %: No, it is obviously not sufficient to keep the last single non-zero digit, and
- %: the problem appears to be a very interesting and intriguing one:
- %:
- %: A new trailing zero is 'created' every time the next multiplier is divisible
- %: by 5, and how can we then calculate the next 'rightmost significant' digit?
- %:
- %: Example when a multiplier ends in 05:
- %:
- %: If the 'previous' factorial ended in 02 then the new factorial will end in 1
- %: while if it ended in 12 then the new factorial will end in 6 (after removal of
- %: trailing zeroes).
- %:
- %: Thus the SINGLE rightmost significant digit in the new factorial depends upon
- %: the TWO rightmost significant digits both in the previous factorial and in the
- %: multiplier.
- %:
- %: This means that we must keep track of the last TWO digits to calculate the new
- %: SINGLE rightmost significant digit whenever a zero is 'created'. For similar
- %: reasons we must track THREE digits to calculate the new TWO rightmost
- %: significant digits - and so on.
- %:
- %: How many zeroes have been 'removed' when we reach 1000! ? The answer is 249,
- %: which means that in order to maintain the precision needed we must track the
- %: last 250 digits less the number of zeroes already 'removed' when N! reaches a
- %: number with that many digits.
- %:
- %: This is where I get stuck. Hey - number theory specialists: How do we proceed?
-
- % I'm far from being a specialist in number theory, but as can be seen from
- % my previous posting in this thread, have dealt with the problem for a while,
- % too. Allow me to give some explanation why the problem can be solved by
- % just keeping two digits stored. Assume we have two such digits m and n:
-
- % x = 10 * m + n; with 0<=m<=9 and 1<=n<=9
-
- % When multiplying to another number y the LSD of the result can be determined
- % by calculating LSD(n*LSD(y)) in most cases (see tabular below).
-
- % n || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
- % LSD(y) || | | | | | | | | |
- % =========##=====#=====#=====#=====#=====#=====#=====#=====#=====#
- % 1 || 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 2 || 2 | 4 | 6 | 8 | * | 2 | 4 | 6 | 8 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 3 || 3 | 6 | 9 | 2 | 5 | 8 | 1 | 4 | 7 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 4 || 4 | 8 | 2 | 6 | * | 4 | 8 | 2 | 6 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 5 || 5 | * | 5 | * | 5 | * | 5 | * | 5 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 6 || 6 | 2 | 8 | 4 | * | 6 | 2 | 8 | 2 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 7 || 7 | 4 | 1 | 8 | 5 | 2 | 9 | 6 | 3 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 8 || 8 | 6 | 4 | 2 | * | 8 | 6 | 4 | 2 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
- % 9 || 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
- % ---------++-----+-----+-----+-----+-----+-----+-----+-----+-----+
-
- % We only are in trouble if one of these two is 5 and the other is even
- % (see entries marked with *). In this case we need one more digit from
- % the even number to determin the new LSD of the result which is:
-
- % LSD(n*LSD(y))+q*5 with q being either m if LSD(y)==5
- % or q being the next digit from y if n==5
-
- % Storing additional digits is not neccessary. If two numbers of at least
- % two digits have no zeroes in their rightmost digit, the two rightmost
- % digits of their product cannot both be zero and are determined from the
- % two rightmost digits of the two numbers. If you don't believe me feel
- % free to expand the tabular above for these numbers. Unfortunately you'll
- % have to write down 8100 entries... :-)
-
- % Ans now we should probably keep this away from comp.lang.c and turn to
- % some algorithmic group with it. Does anyone know one..?
-
- % - Jochen
-
- % --
- % --------------------------------------------------------------------------
- % Jochen Schoof mailto:schoof@informatik.uni-wuerzburg.de
- % Lehrstuhl fuer Informatik II +-------------------------------------------
- % Universitaet Wuerzburg | You are just reading a .sig-light:
- % D-97074 Wuerzburg (Germany) | It is free of fat, sugar and cholesterol!
- % ------------------------------+-------------------------------------------
- % WWW-Homepage: http://www.informatik.uni-wuerzburg.de/staff/joscho
- % --------------------------------------------------------------------------
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-